package com.hunttown.mes.impl;

import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.api.AnalysisManageRightRoleRpcService;
import com.hunttown.mes.rpc.domain.AnalysisManageRightRoleDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisManageRightRoleDTOQuery;
import com.hunttown.mes.service.AnalysisManageRightRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisManageRightRoleConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:05:48
 */
@Service
public class AnalysisManageRightRoleRpcServiceImpl implements AnalysisManageRightRoleRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisManageRightRoleRpcServiceImpl.class);

    private final AnalysisManageRightRoleService analysisManageRightRoleService;

    @Autowired
    public AnalysisManageRightRoleRpcServiceImpl(AnalysisManageRightRoleService analysisManageRightRoleService) {
        this.analysisManageRightRoleService = analysisManageRightRoleService;
    }

    @Override
    public AnalysisManageRightRoleDTO insert(AnalysisManageRightRoleDTO analysisManageRightRoleDTO) {
        try {
            return convert(analysisManageRightRoleService.insert(convert(analysisManageRightRoleDTO)));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRole create failed.", e);
            throw new RuntimeException("AnalysisManageRightRole create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisManageRightRoleDTO analysisManageRightRoleDTO) {
        try {
            return analysisManageRightRoleService.updateInfoById(convert(analysisManageRightRoleDTO));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRole update by id failed.", e);
            throw new RuntimeException("AnalysisManageRightRole update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisManageRightRoleDTOQuery analysisManageRightRoleDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisManageRightRoleDTOQuery.getW_id() != null && analysisManageRightRoleDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisManageRightRoleDTOQuery.getW_id());
            }

            if (analysisManageRightRoleDTOQuery.getIds() != null && !analysisManageRightRoleDTOQuery.getIds().equals("")) {
                query.put("ids", analysisManageRightRoleDTOQuery.getIds());
            }

            if (analysisManageRightRoleDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", analysisManageRightRoleDTOQuery.getStopFlag());
            }

            if (analysisManageRightRoleDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisManageRightRoleDTOQuery.getDeleteFlag());
            }

            return analysisManageRightRoleService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisManageRightRole update by query failed.", e);
            throw new RuntimeException("AnalysisManageRightRole update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageRightRoleDTO selectById(Integer id) {
        try {
            return convert(analysisManageRightRoleService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRole select by id failed.", e);
            throw new RuntimeException("AnalysisManageRightRole select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageRightRoleDTO selectObjByQuery(AnalysisManageRightRoleDTOQuery analysisManageRightRoleDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(analysisManageRightRoleService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRole select obj by query failed.", e);
            throw new RuntimeException("AnalysisManageRightRole select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisManageRightRoleDTO> getListForPage(AnalysisManageRightRoleDTOQuery analysisManageRightRoleDTOQuery) {
        Query query = Query.page(analysisManageRightRoleDTOQuery.getPn(), analysisManageRightRoleDTOQuery.getPageSize());

        if (analysisManageRightRoleDTOQuery.getIds() != null && !analysisManageRightRoleDTOQuery.getIds().equals("")) {
            query.put("ids", analysisManageRightRoleDTOQuery.getIds());
        }

        if (analysisManageRightRoleDTOQuery.getParentId() != null) {
            query.put("parentId", analysisManageRightRoleDTOQuery.getParentId());
        }

        if (StringUtils.isNotBlank(analysisManageRightRoleDTOQuery.getRoleNameEn())) {
            query.put("roleNameEn", analysisManageRightRoleDTOQuery.getRoleNameEn());
        }

        if (analysisManageRightRoleDTOQuery.getIsSuperRole() != null) {
            query.put("isSuperRole", analysisManageRightRoleDTOQuery.getIsSuperRole());
        }

        if (analysisManageRightRoleDTOQuery.getLikeName() != null && !analysisManageRightRoleDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisManageRightRoleDTOQuery.getLikeName());
        }

        if (analysisManageRightRoleDTOQuery.getOrderBy() != null && analysisManageRightRoleDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisManageRightRoleDTOQuery.getOrderBy());
        }

        if (analysisManageRightRoleDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", analysisManageRightRoleDTOQuery.getStopFlag());
        }

        if (analysisManageRightRoleDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", analysisManageRightRoleDTOQuery.getDeleteFlag());
        }

        List<AnalysisManageRightRoleDTO> list = convert(analysisManageRightRoleService.selectByQuery(query));
        Page<AnalysisManageRightRoleDTO> page = Page.from(analysisManageRightRoleDTOQuery.getPn(), analysisManageRightRoleDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisManageRightRoleService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisManageRightRoleService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisManageRightRole delete failed.", e);
            throw new RuntimeException("AnalysisManageRightRole delete failed, Please order your code.", e);
        }
    }
}